package perform.query.impl;

import java.sql.Connection;

import perform.criterion.Criterion;
import perform.criterion.Order;
import perform.exception.QueryException;
import perform.query.AbstractQuery;
import perform.query.builder.QueryBuilder;
import perform.type.Type;

public class SelectQuery extends AbstractQuery {

	public SelectQuery(Class<? extends Type> clazz, Criterion criterion, Connection connection)
			throws QueryException {
		super();
		try {
			StringBuilder builder = new StringBuilder(QueryBuilder.getSelectQuery(clazz))
					.append(criterion.toString()).append(" ;");
			query = builder.toString();
			preparedStatement = connection.prepareStatement(query);
		} catch (Exception e) {
			throw new QueryException(e);
		}
	}
	public SelectQuery(Class<? extends Type> clazz, Criterion criterion,
			Order order, Connection connection)
			throws QueryException {
		super();
		try {
			StringBuilder builder = new StringBuilder(QueryBuilder.getSelectQuery(clazz))
					.append(criterion.toString()).append(" ").append(order.toString()).append(" ;");
			query = builder.toString();
			preparedStatement = connection.prepareStatement(query);
		} catch (Exception e) {
			throw new QueryException(e);
		}
	}
	public SelectQuery(Class<? extends Type> clazz, Criterion criterion,
			Order order, int lowLimit, int highLimit, Connection connection)
			throws QueryException {
		super();
		try {
			StringBuilder builder = new StringBuilder(QueryBuilder.getSelectQuery(clazz))
				.append(criterion.toString()).append(" ").append(order.toString())
				.append(" limit ").append(lowLimit).append(", ").append(highLimit).append(" ;");
			query = builder.toString();
			preparedStatement = connection.prepareStatement(query);
		} catch (Exception e) {
			throw new QueryException(e);
		}
	}
	public SelectQuery(Class<? extends Type> clazz, String criteria, Connection connection)
			throws QueryException {
		super();
		try {
			StringBuilder builder = new StringBuilder(QueryBuilder.getSelectQuery(clazz))
			.append(criteria).append(" ;");
			query = builder.toString();
			preparedStatement = connection.prepareStatement(query);
		} catch (Exception e) {
			throw new QueryException(e);
		}
	}
	public SelectQuery(Class<? extends Type> clazz, String criteria,
			Order order, Connection connection)
			throws QueryException {
		super();
		try {
			StringBuilder builder = new StringBuilder(QueryBuilder.getSelectQuery(clazz))
			.append(criteria).append(" ").append(order.toString()).append(" ;");
			query = builder.toString();
			preparedStatement = connection.prepareStatement(query);
		} catch (Exception e) {
			throw new QueryException(e);
		}
	}
	public SelectQuery(Class<? extends Type> clazz, String criteria,
			Order order, int lowLimit, int highLimit, Connection connection)
			throws QueryException {
		super();
		try {
			StringBuilder builder = new StringBuilder(QueryBuilder.getSelectQuery(clazz))
			.append(criteria).append(" ").append(order.toString())
			.append(" limit ").append(lowLimit).append(", ").append(highLimit).append(" ;");
			query = builder.toString();
			preparedStatement = connection.prepareStatement(query);
		} catch (Exception e) {
			throw new QueryException(e);
		}
	}
	public SelectQuery(String from, Class<? extends Type> clazz, Connection connection) throws QueryException{
		super();
		try {
			StringBuilder builder = new StringBuilder(QueryBuilder.getFromQuery(clazz)).append(from);
			query = builder.toString();
			preparedStatement = connection.prepareStatement(query);
		} catch (Exception e) {
			throw new QueryException(e);
		}
	}

}
